<?php
/**
 * Created by PhpStorm.
 * User: Guangpeng Chen
 * Date: 15-4-3
 * Time: 上午11:10
 */
header('Content-type: application/x-javascript');
define('ROOT_PATH','/var/www/html/new/d/');
define('FX_PATH', ROOT_PATH.'class/');

include '/var/www/html/new/com.inc.php';
include ROOT_PATH . 'common/func.inc.php';
include FX_PATH . 'Member.php';
include FX_PATH . 'SoapInit.class.php';
include FX_PATH . 'AccessRecord.php';
include 'Handler.class.php';
//include '../common/Db.class.php';

$action = $_REQUEST['action'];
$soap   = SoapInit::GetSoapInside();
$memberId = $_SESSION['memberID'];
if (!$memberId){
    exit('{"outcome":-1,"msg":"未登录或登录超时"}');
}

// $auth = array('46854', '24296');


// 

$obj = new \PFT\Withdraw\Handler(new Member($soap, $memberId), $soap, $memberId );
switch($action) {
	
	
	
	 // 申请提现
    case 'apply':
        echo json_encode( $obj->Apply(intval($_POST['money'])) );
        break;
		
		
	// 体现操作处理
    case 'refund':
				
		write_logs1(json_encode($_REQUEST));
		$auth = array('46854', '24296','24322','24436');
		if(!in_array($_SESSION['memberID'], $auth)) exit('{"outcome":-1,"msg":"无权限操作"}');
		
		$logid  = intval($_POST['id']);
		$status = intval($_POST['refund_status']);
		$memo   = isset($_REQUEST['input_txt']) ? safetxt($_REQUEST['input_txt']):'';

		$sql = "select wd_status,wd_money,fid from pft_wd_cash where id=$logid limit 1";
        $GLOBALS['le']->query($sql);
        if(!($w_info=$GLOBALS['le']->fetch_assoc())) exit('{"outcome":-1,"msg":"申请记录不存在"}');
		$dstatus = $w_info['wd_status'];// 表记录状态
		
		if(($status==1 || $status==4) && $dstatus>0) exit('{"outcome":-1,"msg":"记录状态不正确"}'); // 同意提现操作，但记录有误
		if(($status==0 || $status==2) && $dstatus!=1) exit('{"outcome":-1,"msg":"记录状态不正确"}');// 财务转账或撤回，但记录非同意
		
		$wd_operator = "wd_operator='{$_SESSION['dname']}|ID:{$_SESSION['memberID']}'";
		if($status==1) $memo = '同意提现';
		$upd = "update pft_wd_cash set wd_status=$status,memo='$memo',$wd_operator where id=$logid limit 1";
		if($status==2) $upd = "update pft_wd_cash set wd_status=$status,memo='$memo',wd_time=now(),$wd_operator where id=$logid limit 1";
		write_logs1($upd);
		if($GLOBALS['le']->query($upd)){
			$draw_status = array('申请提现', '同意提现', '成功提现', '取消提现', '拒绝提现');
			\AccessRecord::insert(\AccessRecord::ACTION_GET_MONEY, "提现审核|修改状态为:$status-{$draw_status[$status]}|操作人ID:{$_SESSION['memberID']}");
			
			if($status==4){// 拒绝提现，将客户的钱加回去
				$res = $soap->PFT_Member_Fund_Modify($w_info['fid'], $_SESSION['memberID'], $w_info['wd_money'], 0, 0, NULL, 6);
				if($res!=100) exit('{"outcome":-1,"msg":"拒绝提现成功，返款失败，请联系技术"}');
			}
			exit('{"outcome":1,"msg":"操作成功"}');
		}
		exit('{"outcome":-1,"msg":"操作失败"}');
        break;
}

function write_logs1($txt,$file="/var/www/html/new/d/api/logs/data_111.txt"){
	$fp = fopen($file,"a");
	flock($fp, LOCK_EX);
	fwrite($fp,date("Y-m-d H:i:s").":".$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}